<template>
<view>
	<cu-custom bgColor="bg-gradual-blue" :isBack="true">
		<block slot="backText">返回</block>
		<block slot="content">订单详情</block>
	</cu-custom>
	<block v-if="shop.type === 5">
		<view class="detail" v-if="loaded">
			<view class="status">
				<order-status :source="info" :type="2" :o_mx="PayMoney" @on-overtime="info.status = -2"></order-status>
				<view class="addr">
					<block v-if="!isVirtual">
						<view class="flex align-center">
							<view class="flex margin-right-xs"><image src="/static/addr.png"></image><text class="text-black text-df">{{ info.user_name }}</text></view>
							<view class="phone">{{ info.phone }}</view>
						</view>
						<view class="text-sm text-lgrey padding-left-sm padding-top-xs">地址：{{ info.address }}</view>					
					</block>
					<view v-else class="is_virtual">
						虚拟物品
					</view>
				</view>
			</view>
			<view class="shop">
				<view class="tit">
					<image src="/static/shopicon.png"></image>
					{{ title }}商城</view>
			</view>
			<view class="pro padding-sm bg-white">
				<view class="flex align-center" v-for="item in info.shop_orders" :key="item.id">
					<view class="img" @tap="toDetail(item)" hover-class="none">
						<image :src="item.default_imgurl"></image>
					</view>
					<view class="flex-sub">
						<view class="text-df text-cut">{{ item.pname }}</view>
						<view class="rule margin-top-xs">
							<text class="text-xs">数量：x{{ item.rcount }}</text>
							<text class="margin-left-xs">{{ item.name }}</text>
						</view>
						<view class="text-price text-bold text-df margin-top-xs">{{ item.now_price }}</view>
					</view>						
				</view>
			</view>
			<view class="ox">
				<view>
					<view><view>订单编号：</view><view>{{ info.batchcode }}</view></view>
					<view><view>下单时间：</view><view>{{ info.createtime }}</view></view>			
				</view>
				<view v-if = "(info.status === 0 &&  info.sendstatus === 2 && info.is_discuss === 0 || info.status === 1 && info.sendstatus === 2 && info.is_discuss === 0) || (info.status === 0 && info.paystatus === 1 && info.sendstatus === 1) || (info.status === 0 && info.sendstatus === 2 && info.is_discuss === 1 || info.status === 1 && info.sendstatus === 2 && info.is_discuss === 1)">
					<view><view>发货日期：</view><view>{{ info.confirm_sendtime }}</view></view>	
					<view><view>物流公司：</view><view>{{ info.send_express_name }}</view></view>
					<view><view>物流单号：</view><view>{{ info.expressnum }}</view></view>					
				</view>	
			</view>
			<view class="desc">
				<view><view>商品总额：</view><view class="text-price text-bold text-df">{{ info.totalprice }}</view></view>
				<view><view>运费：</view><view>+<text class="text-price text-df">{{ info.sendMoney }}</text></view></view>
				<view v-if="info.deductible_money"><view>积分抵扣：</view><view>-<text class="text-price text-df"></text>{{ info.deductible_money }}</view></view>
				<view v-if="info.pay_currency"><view>购物币抵扣：</view><view>-<text class="text-price text-df"></text>{{ info.pay_currency }}</view></view>
			</view>
			<view class="total"><view><order-status :source="info" :type="1"></order-status>付款：<text class="text-bold text-price text-xl text-red">{{ PayMoney }}</text></view></view>
		</view>		
	</block>
	<block v-else-if="shop.type === 1">
		<view class="bg-white padding solid-bottom">
			<view class="cu-steps">
				<view class="cu-item" :class="index > basics ? '': 'text-red'" v-for="(item,index) in basicsList" :key="index">
					<text :class="'icon-' + item.icon"></text> {{item.name}}
				</view>
			</view>
		</view>
		<view class="list bg-white">
			<view class="shop">
				<view class="head"><text class="lg text-black icon-shopfill"></text> {{ info.shop_name }}</view>
				<view class="item" v-for="(item, i) in info.shop_orders" :key="i">
					<image :src="item.default_imgurl"></image>
					<view class="desc">
						<view class="title"><text>{{ item.pname }}</text></view>
						<view class="attr_name margin-top-xs margin-bottom-xs">{{ item.name }}</view>
						<view class="price">
							<text class="m text-price text-df text-bold">{{ item.costprice }}</text>
							<text class="text-lgrey">x{{ item.count }}</text>
						</view>				
					</view>
				</view>					
			</view>				
		</view>
		<view class="bg-white margin-top-sm">
			<view class="flex justify-between padding-sm solid-bottom">
				<view>订单编号</view>
				<view>{{ info.batchcode }}</view>
			</view>
			<view class="flex justify-between padding-sm">
				<view>创建时间</view>
				<view>{{ info.createtime }}</view>
			</view>			
		</view>
		<view class="bg-white margin-top-sm">
			<view class="flex justify-between padding-sm solid-bottom">
				<view>商品总价</view>
				<view class="text-price text-bold text-df">{{ info.money }}</view>
			</view>
			<view class="flex justify-between padding-sm solid-bottom">
				<view>运费</view>
				<view>+<text class="text-price text-bold text-df">{{ 0 }}</text></view>
			</view>	
			<view class="flex justify-between padding-sm">
				<view>实付款</view>
				<view class="text-price text-df text-red text-bold">{{ info.real_pay }}</view>
			</view>						
		</view>		
	</block>
	<view class="btn"><con-btn :source="info" :type="1"  @onPay="show = true"></con-btn></view>
	<pay-list @on-line="lineCreate" :payWay="payWay" @close-pay="show = false" :show="show" :params="payInfo" :pid="pid"></pay-list>	
</view>
</template>

<script>
	import { orderIndex } from '@/common/api/users'
	import { getProActive, BagOrderDetail } from '@/common/api'	
	import OrderStatus from './components/status'
	import ConBtn from './components/btn'
	import PayList from '@/components/payList'
	import { mapGetters } from 'vuex'
	import { shopOrders } from '@/common/api/shop'
	export default {
		name: 'odetail',
		data () {
			return {
				basicsList: [{
					icon: 'recharge',
					name: '待付款'
				}, {
					icon: 'deliver',
					name: '待发货'
				}, {
					icon: 'deliver_fill',
					name: '配送中'
				},{
					icon: 'squarecheckfill',
					name: '已完成'
				}],
				basics: 0,				
				info: {},
				show: false,
				batchcode: '',
				loaded: false,
				pid: '',
				payWay: 0
			}
		},
		components: { OrderStatus, ConBtn, PayList },
		onLoad (opt) {
			this.payWay = +opt.tag ? +opt.tag : 0
			if (this.shop.type === 5) {
				this.batchcode = opt.batchcode
				this.getList()				
			} else if (this.shop.type === 1) {
				this.info = uni.getStorageSync('onLineDetails')
				if (this.info.status === 4 || this.info.status === 3) {
					this.basics = 3
				} else if (this.info.status === 1 || this.info.status === 9 || this.info.status === 11 || this.info.status === 13) {
					this.basics = 1
				}
			}
		},
		computed: {
			...mapGetters(['shop', 'userInfo', 'navigator']),
			title () {
				return this.navigator.title
			},
			PayMoney () {
				let one = this.utils.accAdd(+this.info.totalprice, this.info.sendMoney)
				let two = this.utils.accSub(one, this.utils.accAdd(this.info.deductible_money, this.info.pay_currency))
				return this.utils.Fxied(two)
			},
			payInfo () {
				return Object.assign({}, { batchcode: this.batchcode }, { total_price: this.PayMoney })
			},
			isVirtual () {
				return !this.info.user_name && !this.info.address
			}
		},
		methods: {
			lineCreate (id) {
				let  pay = [{ id: 1, name: 'card_pay' }, {id: 3, name: 'change_pay'}, { id: 4, name: 'pay_weixin'}]				
				let params = {
					batchcode: this.info.batchcode,
					appid: this.$appid,
					// #ifdef MP-WEIXIN
					openid: this.userInfo.openid,
					// #endif
					op: pay[pay.findIndex(item => item.id === id)].name
				}
				shopOrders(params, this.shop.customer_id).then(res => {
					if (id === 4 || id === 5) {
					 let provider = ''
					 id === 4 ? provider = 'wxpay' : provider = 'alipay'							
					// #ifndef MP-WEIXIN
						 uni.requestPayment({
							 provider: provider,
							 orderInfo: res.data,
							success: (res) => {
								if (this.buy === 1) {
									let list = uni.getStorageSync('ShopCart').map(item => {
										item.list = item.list.filter(k => !k.checked)
										return item
									}).filter(item => item.list.length)
									uni.setStorageSync('ShopCart', list) // 如果是购物车的产品，创建订单完成后清空购物车													
								} 
								uni.redirectTo({
									url: '/pages/orders/list',
								})
							},
							fail: err => {
								console.log(JSON.stringify(err))
							}
						 })
					// #endif
					 // #ifdef MP-WEIXIN
					 uni.requestPayment({
						 provider,
						 ...res,
						 success: r => {
							 
						 },
						 fail: er => {
							 console.log(er)
						 }
					 });
					 // #endif							
				} else {
					if (res.result === 1) {
						uni.showModal({
							content: res.msg,
							showCancel: false,
							success: () => {
							}
						})								
					} else if (res.result === 0) {
						uni.showModal({
							content: res.msg,
							showCancel: false
						})
					}
				}					
				})
			},
			toDetail (item) {
				if (this.payWay !== 6) {
					uni.navigateTo({
						url: '/pages/orders/productinfo?pid='+item.product_id
					})
				}
			},
			getBagInfo () {
				return new Promise(resolve => {
					BagOrderDetail({ batchcode: this.batchcode }).then(res => {
						if (!res.code) {
							const data = res.data[0]
							let info = {}
							info.CouponPrice = data.totalprice
							info.send_express_name = data.expressname
							info.address = data.location_p + data.location_c + data.location_a + data.address
							info.aftersale_type = 0
							info.batchcode = data.batchcode
							info.createtime = data.createtime
							info.confirm_sendtime = data.confirm_sendtime
							info.decrease_money = 0
							info.deductible_money = 0
							info.expressnum = data.expressnum
							info.id = data.p_id
							info.is_discuss = 0
							info.pay_currency = 0
							info.pay_money = data.package_price
							info.paystatus = data.paystatus
							info.phone = data.phone
							info.pid = data.p_id
							info.pname = data.package_name
							info.rcount = data.rcount
							info.return_status = data.return_status
							info.sendMoney = 0
							info.sendstatus = data.sendstatus
							info.shop_orders = [
								{ default_imgurl: data.default_head_imgurl, pname: data.package_name, rcount: data.rcount, now_price: data.package_price }
							]
							info.tag = 6
							info.status = 0
							info.totalprice = data.totalprice
							info.user_name = data.name
							this.info = info
						}
						resolve()
					})					
				})
			},			
			async getList () {
				uni.showLoading({ mask: true })
				if (!this.payWay) {
					const pid = await this.getInfo()
					if (this.info.shop_orders.length === 1) await this.getActive(pid)					
				} else {
					await this.getBagInfo()
				}
			    this.loaded = true
				uni.hideLoading()
			},
			getInfo () {
				return new Promise((resolove, reject) => {
					orderIndex({ batchcode: this.batchcode }).then(res => {
						if (res.length) {
							let data = res.map(item => {
								item.deductible_money = +this.utils.Fxied(item.deductible_money) // 积分抵扣
								item.sendMoney = this.utils.Fxied(item.sendMoney) // 运费
								item.pay_currency = +this.utils.Fxied(item.pay_currency) // 购物币抵扣
								if (item.phone) item.phone = item.phone.substr(0, 3) + '****' + item.phone.substr(7)
								item.shop_orders = item.shop_orders.map(x => {
									x.now_price = this.utils.Fxied(this.utils.accEqu(+x.totalprice, x.rcount))
									if (x.name) x.name = x.name.replace(/-/ig, ' ')
									return x
								})
								return item
							})
							this.info = data[0]
							const pid = this.info.shop_orders.reduce((total, item) => total.concat(item.product_id), []).toString()
							resolove(pid)
						} else {
							uni.showModal({
								content: '请稍后重试',
								success: rst => {
									if (rst.confirm) uni.navigateBack()
								}
							})
							reject('Error')
						}
					})					
				})
			},
			getActive (pid) {
				return new Promise((resolve, reject) => {
					getProActive({ pid }).then(res => {
						if (!res.code) {
							if (res.data.pay_type === 1) this.pid = pid
						}
						resolve()
					})
				})
			}
		}
	}
</script>

<style lang="less">
@import '../../common/styles/variables.less';	
.detail {
	padding-bottom: 100upx;
	.status {
		background: @headBg;		
		color: white;		
		padding: 20upx 15upx 100upx;
		position: relative;		
		& > view {
			&.addr {
				position: absolute;
				left: 15upx;
				right: 15upx;
				bottom: 0;
				transform: translate3d(0, 50%,0);
				background-color: white;
				border-radius: 15upx;
				overflow: hidden;
                box-shadow: 0px 2px 12px 0px #e8e8e8;
				color: black;
				padding: 20upx 15upx;
				image {
					width: 35upx;
					height: 35upx;
					display: block;
					margin-right: 6upx;
				}				
			}			
		}
	}
	.shop {
		padding: 100upx 0 0 0;
		background-color: white;
		.tit {
			height: @height;
			display: flex;
			align-items: center;
			padding: 0 15upx;
			image {
				width: 35upx;
				height: 35upx;
				display: block;
				margin-right: 6upx;
			}
			.border-bottom;
		}
	}
	.pro {
		& > view {
			&:not(:last-of-type) {
				margin-bottom: 15upx;
			}
			.img {
				width: 140upx;
				height: 140upx;
				margin-right: 20upx;									
				image {
					width: 100%;
					height: 100%;
					display: block;
				}
			    & + view { overflow: hidden; }
			}												
		}
	}
	.ox {
		background-color: white;
		margin: 20upx 0;
		padding: 0 15upx;
		& > view {
			padding: 20upx 0;
			&:not(:last-of-type) {
				.border-bottom;
			}
			& > view {
				&:not(:last-of-type) {
					padding-bottom: 10upx;
				}
				display: flex;
				& > view {
					font-size: 24upx;
					color: black;
					font-weight: 600;
					&:nth-of-type(2) {
						font-weight: normal
					}
				}
			}
		}
	}
	.desc {
		background-color: white;
		padding: 10upx 0;
        .border-bottom;		
		& > view {
			display: flex;
			justify-content: space-between;
			padding: 0 15upx 10upx;
			&:last-of-type {
				margin-bottom: 0
			}
		}
	}
	.total {
		padding: 10upx 15upx;
		display: flex;
		background-color: white;
		justify-content: flex-end;
		& > view {
			font-weight: bold;
			font-size:@minSize;
			display: flex;
			align-items: center;			
		}
	}
}
	.btn {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
	}
.list {
	.head {
		padding: 0 15upx;			
		height: @height;
		font-size:@title;
		display: flex;
		align-items: center;
		.lg {
			font-size: 38upx;
			margin-right: 10upx;
		}
		.border-bottom;
		image {
			width: 36upx;
			height: 36upx;
			margin-right: 10upx;
			display: block;
		}			
	}
	.item {
		padding: 20upx 15upx;
		display: flex;
		align-items: flex-start;
		&:not(:last-of-type) { .border-bottom(15upx) }
		image {
			width: 150upx;
			height: 150upx;
			display: block;
		}
		.desc {
			flex: 1;
			display: flex;
			flex-direction: column;
			flex-flow: column;
			justify-content: space-between;
			margin-left: 20upx;
			.title { .twoLine(2) }
			.price {
				display: flex;
				justify-content: space-between;
				align-items: center;
				text {
					&:nth-of-type(1) {
						color:@red
					}
				}
			}
			.attr_name {
				font-size:@minSize;
				color:@gray;
			}
		}			
	}
}
</style>
